<?php
/**
 * YzmCMS 手机模块
 * 手机版如果想实现与PC版一样可自由切换模板方式，查看教程:
 * https://bbs.yzmcms.com/bbs/index/show/id/504.html
 *
 * @author           袁志蒙
 * @license          http://www.yzmcms.com
 * @lastmodify       2020-03-26
 */

defined('IN_YZMPHP') or exit('Access Denied');
yzm_base::load_model('content', 'index', 0);

class index{


	public function __construct() {
		//设置手机模块模板风格
		set_module_theme('default');
	}


	/**
	 * 首页
	 */
	public function init() {
		$site = get_config();
		$seo_title = $site['site_name'];
		$keywords = $site['site_keyword'];
		$description = $site['site_description'];

        //幻灯片
        $banner = D('banner')->where('status=1 and typeid = 3') -> field("title,image,url") -> select();


        include template('mobile', 'index');
	}


    public function getrecommend(){
        header("Access-Control-Allow-Origin: *"); // 允许任意域名发起的跨域请求

        $result = array('errCode' => 0,'errno' => 0,'errMsg' => 'success');

        $article = D('article');
        $article_count = $article->where(['status'=>1])->total();

        $next = $_GET['next'];

        $pagelimit = $next * 20;
        $new_next = $next + 1;

        $list = $article->fetch_all($article->query("SELECT a.url eidEnc,a.catid as vote,a.title,a.thumb as img,a.updatetime,a.nickname as author FROM `yzm_article` a inner join yzm_category b on a.catid=b.catid  ORDER BY a.inputtime Desc limit $pagelimit,20 "));
        foreach($list as $key => &$val){
            $temp = $val;
            $list[$key] = '';
            $val['data'][0] = $temp;
            $val['catid'] = $temp['vote'];
            $c = D('category')->where(['catid' => $temp['vote']])->field('catname tag')->find();
            $val['tag'] = $c['tag'];
        }


        $result['data'] = array(
            'next' => $new_next,
            'list' => $list
        );

        return_json($result);
    }

    /*废弃*/
    public function old_getrecommend(){
        header("Access-Control-Allow-Origin: *"); // 允许任意域名发起的跨域请求

        $result = array('errCode' => 0,'errno' => 0,'errMsg' => 'success');

        $article = D('article');
        $article_count = $article->where(['status'=>1])->total();

        $category = D('category')->where(['parentid' => 0,'display' => 1])->field('catid,catname tag')->select();

        $next = $_GET['next'];
        $type = $_GET['type'];

        switch ($type){
            case '1'://24小时
                //$start = strtotime('-1 days');
                $start = strtotime('-365 days');
                $end = time();
                break;
            case '2'://本周
                //$start = strtotime('-1weekMonday');
                $start = strtotime('-365 days');
                $end = time();
                break;
        }

        $pagelimit = $next * 1;
        $new_next = $next + 1;
        $flag = false;
        foreach($category as $key => &$val){
            /*$val['data'] = $article->fetch_all($article->query("SELECT a.url eidEnc,a.catid as vote,a.title,a.thumb as img,a.updatetime,a.nickname as author FROM `yzm_article` a inner join yzm_category b on a.catid=b.catid where a.updatetime = (select max(updatetime) from yzm_article where catid=a.catid and updatetime BETWEEN ".$start." AND ".$end." )  and FIND_IN_SET(".$val['catid'].",b.arrparentid) ORDER BY a.updatetime Desc limit $pagelimit,1 "));*/
            $val['data'] = $article->fetch_all($article->query("SELECT a.url eidEnc,a.catid as vote,a.title,a.thumb as img,a.updatetime,a.nickname as author FROM `yzm_article` a inner join yzm_category b on a.catid=b.catid where FIND_IN_SET(".$val['catid'].",b.arrparentid) ORDER BY a.updatetime Desc limit $pagelimit,1 "));
            foreach ($val['data'] as $k => &$v){
                $v['img'] = 'http://www.qinxichi.com' . $v['img'];
            }
            if(empty($val['data'])){
                array_splice($category,$key,1);
            }
        }

        if($flag){
            $category = [];
            $new_next = 0;
        }

        $result['data'] = array(
            'next' => $new_next,
            'list' => $category
        );

        return_json($result);
    }

	/**
	 * 栏目列表页
	 */
	public function lists() {
		$catid = isset($_GET['catid']) ? intval($_GET['catid']) : 0;
		if(!$catid) showmsg(L('missing_parameter'),'stop');
		$catinfo = get_category($catid);
		if(!$catinfo) showmsg(L('category_not_existent'),'stop');
		extract($catinfo);

		//外部链接
		if($type == 2) showmsg(L('is_jump'), $pclink, 1);

		//SEO相关设置
		$site = get_config();
		$seo_title = $seo_title ? $seo_title : $catname.'_'.$site['site_name'];
		$keywords = $seo_keywords ? $seo_keywords : $site['site_keyword'];
		$description = $seo_description ? $seo_description : $site['site_description'];

		//手机模板暂时就做这一个，不要问我为什么，因为没时间~~
		$template = 'category_article';

		//单页面
		if($type == 1){
			$r = D('page')->where(array('catid'=>$catid))->find();
			extract($r);
			$template = 'category_page';
		}

		include template('mobile', $template);
	}


	/**
	 * 内容页
	 */
	public function show() {
		$catid = isset($_GET['catid']) ? intval($_GET['catid']) : 0;
		$id = isset($_GET['id']) ? intval($_GET['id']) : 0;
		if(!$catid || !$id) showmsg(L('missing_parameter'),'stop');

		$category = get_category($catid);
		if(!$category) showmsg(L('category_not_existent'),'stop');
		$modelid = $category['modelid'];

		$tablename = get_model($modelid);
		if(!$tablename)  showmsg(L('model_not_existent'),'stop');
		$db = D($tablename);
		$data = $db->where(array('id'=>$id))->find();
		if(!$data || $data['status'] != 1) showmsg(L('content_not_existent'),'stop');
		extract($data);

		//会员组权限和阅读收费检测，手机端直接提示用PC打开浏览
		if($groupids_view || $readpoint) {
			showmsg(L('insufficient_authority_pc'), 'stop');
		}

		//SEO相关设置
		$site = get_config();

		//更新点击量
		$db->update('`click` = `click`+1', array('id' => $id));

		//内容分页
		if(strpos($content, '_yzm_content_page_') !== false){
			$content = content::content_page($content);
		}

		//内容关键字
		if(get_config('keyword_link')){
			$content = content::keyword_content($content);
		}

		//获取相同分类的上一篇/下一篇内容
		$pre = $db->field('id,catid,title')->where(array('id<'=>$id , 'status'=>'1' , 'catid'=>$catid))->order('id DESC')->find();
		$next = $db->field('id,catid,title')->where(array('id>'=>$id , 'status'=>'1', 'catid'=>$catid))->order('id ASC')->find();
		$pre = $pre ? '<a href="'.U('mobile/index/show', array('catid'=>$pre['catid'],'id'=>$pre['id'])).'">'.$pre['title'].'</a>' : L('already_is_first');
		$next = $next ? '<a href="'.U('mobile/index/show', array('catid'=>$next['catid'],'id'=>$next['id'])).'">'.$next['title'].'</a>' : L('already_is_last');

        //方法/步骤
        $step_list = D('article_step')->where(['article_id' => $data['id']])->order('id Asc')->select();

        //步骤图片
        $step_img = [];
        foreach ($step_list as $k => $v){
            $step_img[$k] = $v['step_img'];
        }
        $step_img = json_encode($step_img);



        include template('mobile', 'show_article');
	}

	public function cat(){
        include template('mobile', 'list_cat');
    }

    public function step(){

        $id = isset($_GET['id']) ? intval($_GET['id']) : 0;
        $type = isset($_GET['type']) ? intval($_GET['type']) : 0;

        $article = D("article")->where(['id' => $id])->find();
        $article_step = D("article_step")->where(['article_id' => $id])->order('id Asc')->select();
        $step_count = count($article_step);


        extract($article);
        include template('mobile', 'show_step');
    }

    public function asyn(){
        $result = array('errCode' => 0, 'errno' => 0, 'errMsg' => 'success');
        $page = ($_GET['page'] - 1) * 10;

        $result['data']['list'] = D("article")->where('catid in (' . $_GET['cid'] . ') ')->limit($page . ',10')->field('title,thumb firstimg,click succNum,nickname writer')->order('click Desc')->select();
        $result['data']['intTotal'] = 10;

        return_json($result);
    }


	/**
	 * 手机留言板
	 */
	public function guestbook() {

		//SEO相关设置
		$site = get_config();
		$seo_title = '留言反馈_'.$site['site_name'];
		$keywords = $site['site_keyword'];
		$description = $site['site_description'];
		include template('mobile', 'guestbook');
	}



}